package com.hns.can.energy.web;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.MileContrastService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.energy.vo.ParamVo;

import javax.servlet.http.HttpServletResponse;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 里程对比数据
 * @author Chenl
 *
 */
@Controller
@RequestMapping("/mileContrast")
public class MileContrastController extends BaseController{
    @Autowired
    MileContrastService mileContrastService;

    final private String[] headerNames = new String[]{"数据日期","运营线路","车牌号","车辆编号","客户里程","最早数据时间","最大数据时间","云总线里程",
            "修正值","最早数据时间","最晚数据时间","差值"};
    final private String[] headerFields = new String[]{ "rcrdDate","lineName", "licPltNo", "carInCd","clientRngMile","clientMinRcrdTime",
            "clientMaxRcrdTime", "cloudRngMile","fixMile", "cloudMinRcrdTime", "cloudMaxRcrdTime", "betweenMile"};
    /**
     * 查询
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchMileContrast")
    @ResponseBody
    public Object searchMileContrast(ParamVo vo) throws Exception {
        Pagination page = mileContrastService.searchMileContrast(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }

    /**
     * 二汽里程对比数据导出excel
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportMileContrastExcel")
    public void exportMileContrastExcel(ParamVo vo, HttpServletResponse response) throws Exception {
        List list = mileContrastService.exportMileContrast(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
                Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("里程对比数据", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }

    /**
     * 二汽里程对比数据导出PDF
     * @param vo
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportMileContrastPdf")
    public void exportMileContrastPdf(ParamVo vo, HttpServletResponse response) throws Exception {
        List list = mileContrastService.exportMileContrast(vo);
        if(PubUtil.isEmpty(list)){
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
                Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("里程对比数据", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorPDF(pdfEntity, response);
    }
}
